// pages/issue/issuev.js
var app = getApp()
Page({
  /**
   * 页面的初始数据
   */
  data: {
    pics: [],
    onload: "block",
    content:'',
    uploadUrl: app.uploadUrl,
  },
  gotoShow: function () {//这里是选取图片的方法
    var that = this;
    var pics = that.data.pics;
    wx.chooseImage({
      count: 3, // 最多可以选择的图片张数，默认9
      sizeType: ['original', 'compressed'], // original 原图，compressed 压缩图，默认二者都有
      sourceType: ['album', 'camera'], // album 从相册选图，camera 使用相机，默认二者都有
      success: function (res) {
        wx.showLoading({ title: '上传中', })
        wx.uploadFile({
          url: app.url + '/business/upload',
          filePath: res.tempFilePaths[0],
          name: 'file',
          header: { "Content-Type": "multipart/form-data" },
          success: function (e) {
            console.log(e)
            if (pics.length < 3) {
              that.setData({ onload: "", });
              pics.push(JSON.parse(e.data).src)
              that.setData({ pics: pics })
            } else {
              wx.hideLoading();
              that.setData({ onload: "none", });
              return false;
            }
            wx.hideLoading();
          }
        })
      },
      fail: function () {
        // fail
      },
      complete: function () {
        // complete
      }
    })
  },
  deleteList(e) {
    const index = e.currentTarget.dataset.index;
    let pics = this.data.pics;
    pics.splice(index, 1);
    this.setData({
      pics: pics
    });
    if (pics.length < 3) {
      this.setData({
        onload: "block",
      });
    }
  },
  textareaChange:function(e){
    var that = this
    that.setData({
      content: e.detail.value
    }) 
  },
  sub:function(){
    var pics = this.data.pics.join(); 
    var content = this.data.content; 
    if (!content) {
      wx.showToast({
        title: '内容不能为空',
      })
      return false
    }
    if (!pics){
      wx.showToast({
        title: '图片不能为空',
      })
      return false
    }
    console.log(pics)
    console.log(content)
    wx.request({
      url: app.url + '/user/fabu',
      method:'post',
      data:{
        uid:wx.getStorageSync('uid'),
        pics: pics,
        content: content,
      },
      success:function(res){
        console.log('resrse',res)
        if (res.data.data == 1){
          wx.showToast({
            title: '发布成功',
            success:function(){
              wx.navigateTo({
                url: '../index/index',
              })
            }
          })
        }
      }
    }) 
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
  
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  }
})